<template>
  <div class="app-container">
    <!--  搜索栏  -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="houseTitle">
        <el-input
          v-model="queryParams.houseTitle"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房屋地址" prop="houseAddress">
        <el-input
          v-model="queryParams.houseAddress"
          placeholder="请输入房屋地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房屋性质" prop="houseNature">
        <el-select
          v-model="queryParams.houseNature"
          placeholder="出租/出售"
          clearable
          style="width: 180px"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.mini_house_nature"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="朝向" prop="houseToward">
        <el-select
          v-model="queryParams.houseToward"
          placeholder="朝向"
          clearable
          style="width: 180px"
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.mini_house_toward"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="联系人" prop="contacts">
        <el-input
          v-model="queryParams.contacts"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- -----------按钮栏--------------------------------------------------------------- -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mini:house:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mini:house:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mini:house:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mini:house:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="login"
          v-hasPermi="['mini:house:wx']"
        >微信</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- -----------表格--------------------------------------------------------------- -->
    <el-table v-loading="loading" :data="houseList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" width="40"/>
      <el-table-column label="房屋性质" align="center" >
        <template slot-scope="scope">
          <div>{{changeHouseNature(scope.row.houseNature)}}</div>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="houseTitle" />
      <el-table-column label="房屋地址" align="center" prop="houseAddress" />
      <el-table-column label="房屋面积" align="center" >
        <template slot-scope="scope">
          <div>{{scope.row.houseArea}}m²</div>
        </template>
      </el-table-column>
      <el-table-column label="户型" align="center" prop="houseType" />
      <el-table-column label="楼层" align="center" >
        <template slot-scope="scope">
          <div>{{scope.row.houseHeight}}楼</div>
        </template>
      </el-table-column>
      <el-table-column label="朝向" align="center" >
        <template slot-scope="scope">
          <div>{{changeHouseToward(scope.row.houseToward)}}</div>
        </template>
      </el-table-column>
      <el-table-column label="装修类型" align="center">
        <template slot-scope="scope">
          <div>{{changeHouseMount(scope.row.houseMount)}}</div>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="housePic" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.housePic" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="售价" align="center" >
      <template slot-scope="scope">
        <div v-if="scope.row.houseNature == '0'">
          {{scope.row.housePrice}}元/月
        </div>
        <div v-if="scope.row.houseNature == '1'">
          {{scope.row.housePrice}}万
        </div>
      </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="contacts" />
      <el-table-column label="联系电话" align="center" prop="telephone" />
      <el-table-column label="浏览人数" align="center" prop="lookNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mini:house:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mini:house:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- -----------分页--------------------------------------------------------------- -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!------------- 添加或修改房子租售对话框 ------------------------------------------------>
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图片" prop="housePic">
          <image-upload v-model="form.housePic"/>
        </el-form-item>
        <el-form-item label="房屋性质" prop="houseNature">
          <el-select
            v-model="form.houseNature"
            placeholder="房屋性质"
            clearable
            style="width: 180px"
          >
            <el-option
              v-for="dict in dict.type.mini_house_nature"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="houseTitle">
          <el-input v-model="form.houseTitle" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="房屋地址" prop="houseAddress">
          <el-input v-model="form.houseAddress" placeholder="请输入房屋地址" />
        </el-form-item>
        <el-form-item label="房屋面积" prop="houseArea">
          <el-input style="max-width: 60%" v-model="form.houseArea" placeholder="请输入房屋面积" />
          单位：平方米(m^2)
        </el-form-item>
        <el-form-item label="户型" prop="houseType">
          <el-input v-model="form.houseType" placeholder="例如：三房一厅一厨一卫" />
        </el-form-item>
        <el-form-item label="所在楼层" prop="houseHeight">
          <el-select
            v-model="form.houseHeight"
            placeholder="楼层"
            clearable
            style="width: 180px"
          >
            <el-option
              v-for="dict in dict.type.mini_house_height"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="朝向" prop="houseToward">
          <el-select
            v-model="form.houseToward"
            placeholder="朝向"
            clearable
            style="width: 180px"
          >
            <el-option
              v-for="dict in dict.type.mini_house_toward"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="装修类型" prop="houseMount">
          <el-select
            v-model="form.houseMount"
            placeholder="装修类型"
            clearable
            style="width: 180px"
          >
            <el-option
              v-for="dict in dict.type.mini_house_mount"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <div v-if="form.houseNature == '1'">
          <el-form-item label="售价" prop="housePrice">
            <el-input style="max-width: 60%" v-model="form.housePrice" placeholder="68.8万" />
            单位：万
          </el-form-item>
        </div>
        <div v-if="form.houseNature == '0'">
          <el-form-item label="月租" prop="housePrice">
            <el-input style="max-width: 60%" v-model="form.housePrice" placeholder="888"  />
            单位：每月
          </el-form-item>
        </div>

        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入联系电话" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listHouse, getHouse, delHouse, addHouse, updateHouse, wxLogin} from "@/api/mini/house/house";

export default {
  name: "House",
  dicts: ['mini_house_height','mini_house_toward','mini_house_mount','mini_house_nature'],
  data() {
    return {
      // 弹窗的图片
      housePic: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房子租售表格数据
      houseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 上传图片弹窗
      picVisible: false,
      // 上传图片显示
      fileList:[
        //{name:'', url:''}
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseNature: null,
        houseTitle: null,
        houseAddress: null,
        houseArea: null,
        houseType: null,
        houseHeight: null,
        houseToward: null,
        houseMount: null,
        housePic: null,
        housePrice: null,
        contacts: null,
        telephone: null,
        lookNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        houseNature:[{required: true, message:"请选择性质",trigger:"blur"}],
        houseTitle:[{required: true, message:"请输入标题",trigger:"blur"}],
        houseAddress:[{required: true, message:"请输入地址",trigger:"blur"}],
        houseArea:[{required: true, message:"请输入面积",trigger:"blur"},
          {pattern: /^[1-9]\d{0,3}$/,message: "限制4位整数", trigger: 'blur'}],
        housePrice:[{required: true, message:"请输入价格",trigger:"blur"}],
        contacts:[{required: true, message:"请输入联系人",trigger:"blur"}],
        telephone:[
          {required: true, message:"请输入联系电话",trigger:"blur"},
          { pattern: /^1(3|4|5|7|8|9)\d{9}$/, message: '请输入正确的11位手机号码', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    login(){
      wxLogin({
        appid:'wx95c10d9f4a6d89ff',
      }).then(res=>{
        console.log("res",res)
      })
    },
    /** 查询房子租售列表 */
    getList() {
      this.loading = true;
      listHouse(this.queryParams).then(response => {
        this.houseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 新增修改的取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 上传图片 关闭
    closePic() {
      this.picVisible = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        houseNature: null,
        houseTitle: null,
        houseAddress: null,
        houseArea: null,
        houseType: null,
        houseHeight: null,
        houseToward: null,
        houseMount: null,
        housePic: null,
        housePrice: null,
        contacts: null,
        telephone: null,
        lookNum: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房子租售";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHouse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房子租售";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除房子租售编号为"' + ids + '"的数据项？').then(function() {
        return delHouse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mini/house/export', {
        ...this.queryParams
      }, `house_${new Date().getTime()}.xlsx`)
    },
    /** 房屋性质数据转换 */
    changeHouseNature(value){
      if (value == 0){
        return '出租'
      }else if (value == 1){
        return '出售'
      }else {
        return '暂无'
      }
    },
    /** 朝向数据转换*/
    changeHouseToward(value){
      if (value == 0){
        return '朝南'
      }else if (value == 1){
        return '朝东'
      }else if (value == 2){
        return '朝西'
      }else if (value == 3){
        return '朝北'
      }else {
        return '暂无'
      }
    },
    changeHouseMount(value){
      if (value == 0){
        return '精致装修'
      }else if (value == 1){
        return '普通装修'
      }else {
        return '暂无'
      }
    },
  }
};
</script>
